/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.GatherBillBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.CodeRuleDao;
import com.minierp.dao.CooperBillItemDao;
import com.minierp.dao.GatherBillDao;
import com.minierp.dao.GatherBillItemDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.MakeTypeDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.EncGatherBillFormCtrHelper;
import com.minierp.model.CodeRule;
import com.minierp.model.CooperBill;
import com.minierp.model.Department;
import com.minierp.model.Establishment;
import com.minierp.model.GatherBill;
import com.minierp.model.TallySet;
import com.minierp.service.GatherBillService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class GatherBillFormCtr extends SimpleFormController {
	private GatherBillItemDao gatherBillItemDao;
	private GatherBillDao gatherBillDao;
	private CooperBillItemDao cooperBillItemDao;
	private MakeTypeDao makeTypeDao;
	private CodeRuleDao codeRuleDao;
	private SerialNumberDao serialNumberDao;
	private IdGenerator idGenerator;
	private GatherBillService gatherBillService;
	private DataSource dataSource;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		String id = request.getParameter("id");
		if (id == null) {
			id = "";
		}

		refData.put("id", id);
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		String id = request.getParameter("id");
		CodeRule cr = this.codeRuleDao.get("GATHER_BILL");
		if (cr == null) {
			throw new Exception("Code rule not found for gather bill");
		} else {
			GatherBillBean form;
			CooperBill cooperBill;
			List makeTypeList;
			if (id != null && !id.equals("")) {
				GatherBill cooperBillId1 = (GatherBill) this.gatherBillDao.get(
						GatherBill.class, Long.parseLong(id));
				if (cooperBillId1 == null) {
					throw new Exception("Record not found for id:" + id);
				}

				cooperBill = (CooperBill) this.gatherBillDao.get(
						CooperBill.class, cooperBillId1.getCooperBillId());
				if (cooperBill == null) {
					throw new Exception("Cooper bill not found for id:"
							+ cooperBillId1.getCooperBillId());
				}

				TallySet est1 = (TallySet) this.gatherBillDao.get(
						TallySet.class, cooperBillId1.getTallySetId());
				Establishment dep1 = (Establishment) this.gatherBillDao.get(
						Establishment.class, cooperBill.getCooperatorId());
				Department bbItemList2 = (Department) this.gatherBillDao.get(
						Department.class, cooperBill.getDepartmentId());
				makeTypeList = this.gatherBillItemDao
						.getListByGatherBill(cooperBillId1.getId());
				List bbItemList1 = this.cooperBillItemDao.getList(cooperBill
						.getId());
				List makeTypeList1 = this.makeTypeDao.getList(false);
				form = EncGatherBillFormCtrHelper.doFormBackingObjectEdit(cr,
						cooperBillId1, makeTypeList, dep1, bbItemList2,
						cooperBill.getBillCode(), cooperBill.getManagerName(),
						cooperBill.getFavCost(), bbItemList1, makeTypeList1,
						est1);
			} else {
				String cooperBillId = request.getParameter("cooper_bill_id");
				cooperBill = (CooperBill) this.gatherBillDao.get(
						CooperBill.class, Long.parseLong(cooperBillId));
				if (cooperBill == null) {
					throw new Exception("Cooper bill not found for id:"
							+ cooperBillId);
				}

				Establishment est = (Establishment) this.gatherBillDao.get(
						Establishment.class, cooperBill.getCooperatorId());
				if (est == null) {
					throw new Exception("Can\'t find cooperator for id:"
							+ cooperBill.getCooperatorId());
				}

				Department dep = (Department) this.gatherBillDao.get(
						Department.class, cooperBill.getDepartmentId());
				List bbItemList = this.cooperBillItemDao.getList(cooperBill
						.getId());
				makeTypeList = this.makeTypeDao.getList(false);
				form = EncGatherBillFormCtrHelper.doFormBackingObjectNew(
						request, cr, cooperBill, est, dep, bbItemList,
						makeTypeList);
			}

			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		GatherBillBean form = (GatherBillBean) command;
		EncGatherBillFormCtrHelper.doOnBind(form, request, this.dataSource);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		ModelAndView mv = new ModelAndView(this.getSuccessView());
		String id = request.getParameter("id");
		String examine = request.getParameter("examine");
		UserBean ub = EncCommonHelper.getCurrentUserAccount(request);
		GatherBillBean form = (GatherBillBean) command;
		GatherBill obj = form.getGatherBill();
		if (examine != null && examine.equals("1")) {
			if (!EncCommonHelper.checkPrivilege(request, "GATH_EXAM")) {
				throw new Exception("Permission deny!");
			}

			obj.setExamined("Y");
			if (ub.getStaffer() != null) {
				obj.setExaminerId(ub.getStaffer().getId());
				obj.setExaminerName(ub.getStaffer().getStafferName());
			}
		} else {
			obj.setExamined("N");
		}

		if (id != null && !id.equals("")) {
			if (!EncCommonHelper.checkPrivilege(request, "GATH_BILL")) {
				throw new Exception("Permission deny!");
			}

			try {
				this.gatherBillService.txUpdate(form, this.gatherBillDao,
						this.gatherBillItemDao, this.cooperBillItemDao,
						this.idGenerator);
			} catch (CannotAcquireLockException arg11) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg12) {
				EncCommonHelper.setAlertMessage(request, arg12.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		} else {
			try {
				this.gatherBillService.txInsert(form, this.gatherBillDao,
						this.gatherBillItemDao, this.cooperBillItemDao,
						this.serialNumberDao, this.idGenerator);
			} catch (CannotAcquireLockException arg13) {
				EncCommonHelper.setAlertMessage(request, "事务冲突故障，请重新操作！");
				return this.showForm(request, errors, this.getFormView());
			} catch (NotAllowException arg14) {
				EncCommonHelper.setAlertMessage(request, arg14.getMessage());
				return this.showForm(request, errors, this.getFormView());
			}
		}

		mv.addObject("returnValue", String.valueOf(obj.getId()));
		return mv;
	}

	public CodeRuleDao getCodeRuleDao() {
		return this.codeRuleDao;
	}

	public void setCodeRuleDao(CodeRuleDao codeRuleDao) {
		this.codeRuleDao = codeRuleDao;
	}

	public GatherBillDao getGatherBillDao() {
		return this.gatherBillDao;
	}

	public void setGatherBillDao(GatherBillDao gatherBillDao) {
		this.gatherBillDao = gatherBillDao;
	}

	public GatherBillItemDao getGatherBillItemDao() {
		return this.gatherBillItemDao;
	}

	public void setGatherBillItemDao(GatherBillItemDao gatherBillItemDao) {
		this.gatherBillItemDao = gatherBillItemDao;
	}

	public CooperBillItemDao getCooperBillItemDao() {
		return this.cooperBillItemDao;
	}

	public void setCooperBillItemDao(CooperBillItemDao cooperBillItemDao) {
		this.cooperBillItemDao = cooperBillItemDao;
	}

	public MakeTypeDao getMakeTypeDao() {
		return this.makeTypeDao;
	}

	public void setMakeTypeDao(MakeTypeDao makeTypeDao) {
		this.makeTypeDao = makeTypeDao;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public SerialNumberDao getSerialNumberDao() {
		return this.serialNumberDao;
	}

	public void setSerialNumberDao(SerialNumberDao serialNumberDao) {
		this.serialNumberDao = serialNumberDao;
	}

	public GatherBillService getGatherBillService() {
		return this.gatherBillService;
	}

	public void setGatherBillService(GatherBillService gatherBillService) {
		this.gatherBillService = gatherBillService;
	}

	public DataSource getDataSource() {
		return this.dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
}